<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="任务编号" prop="taskCode">
        <el-input
          v-model="queryParams.taskCode"
          placeholder="请输入任务编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务环节名称" prop="taskLinkName">
        <el-select v-model="queryParams.taskLinkName" placeholder="请选择任务环节名称" clearable>
          <el-option
            v-for="dict in dict.type.task_process_link_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="taskLinkStartTime">
        <el-date-picker clearable
          v-model="queryParams.taskLinkStartTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="完成时间" prop="taskLinkFinishTime">
        <el-date-picker clearable
          v-model="queryParams.taskLinkFinishTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="完成时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable>
          <el-option
            v-for="dict in dict.type.process_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['task:taskProcess:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['task:taskProcess:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskProcessList" @selection-change="handleSelectionChange">
      <el-table-column label="任务编号" align="center" prop="taskCode" width="200" />
      <el-table-column label="接收内容" align="center" :show-overflow-tooltip="true"  prop="receiveContent"/>
      <el-table-column label="应答内容" align="center" :show-overflow-tooltip="true"  prop="respContent" />
      <el-table-column label="发送内容" align="center" :show-overflow-tooltip="true"  prop="sendContent" />
      <el-table-column label="任务环节" align="center" prop="taskLinkName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.task_process_link_name" :value="scope.row.taskLinkName"/>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="taskLinkStartTime" width="180" />
      <el-table-column label="完成时间" align="center" prop="taskLinkFinishTime" width="180" />
      <el-table-column label="处理状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.process_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            
            @click="handleRetryCallback(scope.row.id)"
          >重试</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务日志对话框 -->
    <el-dialog title="查看任务日志" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="任务编号" prop="taskCode">
          <el-input v-model="form.taskCode" placeholder="请输入任务编号" disabled/>
        </el-form-item>
        <el-form-item label="接收内容" prop="receiveContent">
          <el-input v-model="form.receiveContent" type="textarea" placeholder="请输入内容" disabled/>
        </el-form-item>
        <el-form-item label="应答内容" prop="respContent">
          <el-input v-model="form.respContent" type="textarea" placeholder="请输入内容" disabled/>
        </el-form-item>
        <el-form-item label="发送内容" prop="sendContent">
          <el-input v-model="form.sendContent" type="textarea" placeholder="请输入内容" disabled/>
        </el-form-item>
        <el-form-item label="环节名称" prop="taskLinkName">
          <el-select v-model="form.taskLinkName" placeholder="任务环节" disabled>
            <el-option
              v-for="dict in dict.type.task_process_link_name"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间" prop="taskLinkStartTime" >
          <el-input v-model="form.taskLinkStartTime" disabled />
        </el-form-item>
        <el-form-item label="完成时间" prop="taskLinkFinishTime">
          <el-input v-model="form.taskLinkFinishTime" disabled />
        </el-form-item>
        <el-form-item label="处理状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择处理状态" disabled>
            <el-option
              v-for="dict in dict.type.process_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listTaskProcess, getTaskProcess, delTaskProcess, addTaskProcess, updateTaskProcess ,retryCallback } from "@/api/task/taskProcess";

export default {
  name: "TaskProcess",
  dicts: ['process_status', 'task_process_link_name'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务日志表格数据
      taskProcessList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskCode: null,
        receiveContent: null,
        respContent: null,
        sendContent: null,
        taskLinkName: null,
        nextTaskLink: null,
        taskLinkStartTime: null,
        taskLinkFinishTime: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskCode: [
          { required: true, message: "任务编号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "处理状态不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询任务日志列表 */
    getList() {
      this.loading = true;
      listTaskProcess(this.queryParams).then(response => {
        this.taskProcessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskCode: null,
        receiveContent: null,
        respContent: null,
        sendContent: null,
        taskLinkName: null,
        nextTaskLink: null,
        taskLinkStartTime: null,
        taskLinkFinishTime: null,
        status: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTaskProcess(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改任务日志";
      });
    },
    handleRetryCallback(id){
      retryCallback(id).then( res => {
        if(res.success) this.$modal.msgSuccess("操作成功");
        else this.$modal.msgError("操作失败");
        this.getList()
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTaskProcess(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTaskProcess(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除任务日志编号为"' + ids + '"的数据项？').then(function() {
        return delTaskProcess(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('task/taskProcess/export', {
        ...this.queryParams
      }, `taskProcess_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
